package com.geekjam.dingdongandroid.Tab;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.geekjam.dingdongandroid.DingDongApp;
import com.geekjam.dingdongandroid.R;
import com.geekjam.dingdongandroid.api.DingDongMessageProxy;
import org.json.JSONObject;

import java.io.IOException;

/**
 * by SEGFault Xiawei
 * Date: 3/9/13 Time: 10:12 PM
 */
public class MessageTab extends LinearLayout{
    private Context context;

    private static final String LOG_TAG = "DingDong";
    private static String mFileName = DingDongMessageProxy.RECORD_FILENAME;

    private ImageView messageIcon;
    private MediaRecorder mRecorder = null;
    private boolean isRecording = false;

    private MediaPlayer mPlayer = null;
    private final ViewAnimator viewAnimator;

    public MessageTab(Context context, ViewAnimator viewAnimator) {
        super(context);
        this.context = context;
        this.viewAnimator = viewAnimator;
        init();
    }

    private void init() {
        inflate(context, R.layout.message, this );

        Button playButton = (Button) findViewById(R.id.play_button);
        playButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startPlaying();
            }
        });
        Button sendButton = (Button) findViewById(R.id.send_button);
        sendButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                DingDongMessageProxy.uploadSound(new DingDongMessageProxy.OnUploadComplete() {
                    @Override
                    public void succeeded() {
                        viewAnimator.setDisplayedChild(DingDongApp.ALARM);
                    }

                    @Override
                    public void failed(Throwable e, JSONObject response) {
                        Toast.makeText(context,"Sending Failed",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void failed(Throwable e, String response) {
                        Toast.makeText(context,"Sending Failed",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });


    }

    private void startPlaying() {
        mPlayer = new MediaPlayer();
        try {
            mPlayer.setDataSource(mFileName);
            mPlayer.prepare();
            mPlayer.start();
            mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    stopPlaying();
                }
            });
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }
    }

    private void stopPlaying() {
        mPlayer.release();
        mPlayer = null;
    }

    public void finalize() {
        if (mRecorder != null) {
            mRecorder.release();
            mRecorder = null;
        }

        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }
}
